<?php

/**
 * 后台系统配置参数模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: config.class.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class config extends Model
{

    private $config_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->config_table = '#@__sysconfig';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addConfig($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->config_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个系统配置参数信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getConfigInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->config_table WHERE sys_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getConfigList($url = null)
    {
        $rs = $this->db->getAll("SELECT * FROM $this->config_table ORDER BY `sys_order` DESC, sys_id ASC");

        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                if ($v['type'] == 'select') {
                    $select_option = null;
                    //接口地址
                    $cfg_apiurl_ary = UtilityConfig::cfg_apiurl_ary();
                    $select_option = Utility::Option($cfg_apiurl_ary, $v['value']);
                    $rs[$k]['select'] = '<select name="' . $v['varname'] . '" id="' . $v['varname'] . '">' . $select_option . '</select>';
                } elseif ($v['type'] == 'radio') {
                    $radio_option_ary = explode('{|}', $v['item']);
                    $cbox = null;
                    //遍历item值
                    foreach ($radio_option_ary AS $kk => $vv) {
                        $radio_option_array = explode(':', $vv);
                        $checked = $radio_option_array[1] == $v['value'] ? 'checked' : null;
                        $cbox .= "<input type='radio' name='{$v['varname']}' value='{$radio_option_array[1]}' {$checked} {$v['nameaction']} />&nbsp;{$radio_option_array[0]}";
                    }
                    $rs[$k]['radio'] = $cbox;
                }
            }
        }
        $this->assign('rs', $rs);
        return $rs;
    }

    /**
     * 写静态配置文件config.cache.php
     */
    public function ReWriteConfig($configfile)
    {
        if (!is_file($configfile)) {
            file_put_contents($configfile, '', LOCK_EX);
        }
        if (!is_writeable($configfile)) {
            echo "配置文件'{$configfile}'不支持写入，无法修改系统配置参数！";
            exit();
        }
        $fp = fopen($configfile, 'w');
        flock($fp, 3);
        fwrite($fp, "<" . "?php\r\n");
        $getconfig = $this->db->getAll("select * from $this->config_table");
        foreach ($getconfig as $k => $v) {
            fwrite($fp, "\$config['config']['{$v['varname']}'] = '" . $v['value'] . "';\r\n");
        }
        fwrite($fp, "?" . ">");
        fclose($fp);
    }

}